package helper

import (
	"github.com/parnurzeal/gorequest"
)

// HttpClient 混入, 可直接调用其方法
type HttpClient struct {
	Token string
	*gorequest.SuperAgent
}

// NewHttpClient 新建请求客户端
func NewHttpClient() *HttpClient {
	cli := gorequest.New()
	return &HttpClient{SuperAgent: cli}
}

func (t *HttpClient) SetToken(token string) *HttpClient {
	t.Token = token
	return t
}

// Get 请求
func (t *HttpClient) Get(url string, params interface{}) ([]byte, error) {
	_, dt, errs := t.SuperAgent.Get(url).Send(params).EndBytes()

	if len(errs) > 0 {
		return nil, LogError("短信发送错误", errs)
	}

	return dt, nil
}

// GetWithResult Get结果到 res
// url search string 包含到 url 中
func (t *HttpClient) GetWithResult(url string, res interface{}) error {
	_, _, errs := t.SuperAgent.Get(url).EndStruct(res)

	if len(errs) > 0 {
		return LogError("短信发送错误", errs)
	}

	return nil
}
